package bingo.common.utils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;

/**
 * Jul 24, 2008 4:21:21 PM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:带有缓存功能的ParameterNameDiscover,用具检查一个给定的方法所有的参数名称
 * ChangeLog:
 */
public class CacheabledParameterNameDiscover {
	private ParameterNameDiscoverer pnd = new LocalVariableTableParameterNameDiscoverer();
	private Map<Method,String[]> paramNameCaches = new HashMap<Method, String[]>();
	private CacheabledParameterNameDiscover(){}
	private static CacheabledParameterNameDiscover instance = new CacheabledParameterNameDiscover();
	
	
	/**
	 * Return parameter names for this method,
	 * or <code>null</code> if they cannot be determined.
	 * @param method method to find parameter names for
	 * @return an array of parameter names if the names can be resolved,
	 * or <code>null</code> if they cannot
	 */
	public String[] getParameterNames(Method method) {
		if(!paramNameCaches.containsKey(method)){
			synchronized (paramNameCaches) {
				if(!paramNameCaches.containsKey(method)){
					paramNameCaches.put(method, pnd.getParameterNames(method));
				}
			}
		}
		return paramNameCaches.get(method);
	}
	
	/**
	 * Return parameter names for this method,
	 * or <code>null</code> if they cannot be determined.
	 * @param method method to find parameter names for
	 * @return an array of parameter names if the names can be resolved,
	 * or <code>null</code> if they cannot
	 */
	public static String[] getMethodParameterNames(Method method) {
		return instance.getParameterNames(method);
	}
	

}
